﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
//using System.Data;

namespace APPLICATION.UserManager
{
    public partial class FrmAddEditRole : Form
    {
        public FrmAddEditRole()
        {
            InitializeComponent();
        }
        public bool ForEdit;
        public string RolesID;

        
        private System.Data.DataView dw;

        private bool check()
        {
            if (textBoxRole.isEmpty)
            {
                MessageBox.Show("Seiyvane rolis dasaxeleba");
            }
            return textBoxRole.isEmpty;
        }

        private bool save()
        {
            UMdataAccess DataAccess=new UMdataAccess();
            if (check())
            {
                return false;
            }
            SqlParameter[] Sqlpar =new SqlParameter[3];

            Sqlpar[1] = new SqlParameter("@RolesN", SqlDbType.NVarChar,100);
            Sqlpar[1].Value = textBoxRole.Text;

            if (ForEdit)
            {
                Sqlpar[0] = new SqlParameter("@Action", SqlDbType.Int);
                Sqlpar[0].Value = 1;

                Sqlpar[2] = new SqlParameter("@RolesID", SqlDbType.Int);
                Sqlpar[2].Direction = ParameterDirection.InputOutput;
                Sqlpar[2].Value = RolesID;
            }
            else
            {
                Sqlpar[0] = new SqlParameter("@Action", SqlDbType.Int);
                Sqlpar[0].Value = 0;

                Sqlpar[2] = new SqlParameter("@RolesID", SqlDbType.Int);
                Sqlpar[2].Direction = ParameterDirection.InputOutput;
                Sqlpar[2].Value = DBNull.Value;
            }
            return DataAccess.addOrUpdRole(Sqlpar,bindingSourceRole);
        }

        private void addnewRole()
        {
            ForEdit = false;
            bindingSourceRole.RemoveFilter();
            dw.RowStateFilter = DataViewRowState.Added;
            if (dw.Count == 0)
            {
                bindingSourceRole.AddNew();               
            }
            else
            {
                MessageBox.Show("sheikvane monacemi jer ertis da mere cadi meoreze she kuta!");              
            }
        }

        private void FrmAddEditRole_Load(object sender, EventArgs e)
        {            
            dw = new DataView(GlobalDataFile.UserManagerDataSet.stRoles);
            bindingSourceRole.DataSource = dw;
            textBoxRole.DataBindings.Add("Text", bindingSourceRole, "RolesN");
            if (ForEdit)
            {
                bindingSourceRole.Filter = "RolesID=" + RolesID;
            }
            else
            {
                this.addnewRole();
            }
        }

        private void FrmAddEditRole_FormClosing(object sender, FormClosingEventArgs e)
        {
            bindingSourceRole.CancelEdit();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.save())
            {
                this.Close();
            }
        }

        private void BtnSaveAndNew_Click(object sender, EventArgs e)
        {
            if (this.save())
            {
                this.addnewRole();
            }
        }
    }
}
